import * as THREE from 'three'
import { TWEEN } from 'three/examples/jsm/libs/tween.module.min';



export function makeCenter(object) {
    object.geometry.computeBoundingBox()
    let centroid = new THREE.Vector3(0, 0, 0);
    centroid.addVectors(object.geometry.boundingBox.min, object.geometry.boundingBox.max);
    centroid.multiplyScalar(.5);
    object.geometry.center();
    object.geometry.applyMatrix4(new THREE.Matrix4().makeTranslation(0, object.geometry.boundingBox.max.y, 0));
    centroid.y -= ((object.geometry.boundingBox.max.y - object.geometry.boundingBox.min.y) / 2);
    object.userData.position = centroid;
    object.position.copy(centroid);
}

export function controlsMoveCoordin(controls,position,target) {
    let tween = new TWEEN.Tween({
        px: controls.object.position.x, // 起始相机位置x
        py: controls.object.position.y, // 起始相机位置y
        pz: controls.object.position.z, // 起始相机位置z
        tx: controls.target.x, // 控制点的中心点x 起始目标位置x
        ty: controls.target.y, // 控制点的中心点y 起始目标位置y
        tz: controls.target.z // 控制点的中心点z 起始目标位置z
    })
    tween.to({
        px: position.x,
        py: position.y,
        pz: position.z,
        tx: target.x,
        ty: target.y,
        tz: target.z
    }, 1000)
    tween.onUpdate( function( pos ) {
        controls.object.position.x = pos.px;
        controls.object.position.y = pos.py;
        controls.object.position.z = pos.pz;
        controls.target.x = pos.tx;
        controls.target.y = pos.ty;
        controls.target.z = pos.tz;
    })
    tween.easing( TWEEN.Easing.Cubic.InOut );
    tween.start();
}


